package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.UserDao;
import com.qf.dao.impl.GoodsDaoImpl;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.domain.Goods;
import com.qf.domain.GoodsType;
import com.qf.domain.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeservice;
import com.qf.web.controller.GoodsTypeServlet;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public PageBean<Goods> findByPage(int page_num, int page_size, String where, List<Object> params) {
        long totaSize = goodsDao.getCount(where,params);
        List<Goods> data = goodsDao.selectByPage(page_num,page_size,where,params);
        // public PageBean(int pageNum, int pageSize, long totaSize, List<T> data)
        PageBean<Goods> pageBean = new PageBean<>(page_num,page_size,totaSize,data);
        return pageBean;
    }

    @Override
    public Goods findById(Integer gid) {
        Goods goods = goodsDao.selectById(gid);
        //根据商品类型id，查找商品类型
        if (goods != null) {
            Integer typeid = goods.getTypeid();
            //调用业务层，通过获得的typeid，找到商品类别表中的商品信息
            GoodsTypeservice goodsTypeservice = new GoodsTypeserviceImpl();
            GoodsType goodsType = goodsTypeservice.findById(typeid);//通过typeid在商品类别表中获取的商品信息
            goods.setGoodsType(goodsType);//将得到的商品信息存入goodsType表中的goodsType
        }
        return goods;
    }

    @Override
    public void add(Goods goods) {
        goodsDao.insert(goods);
    }

  //后加:后台查询商品
    @Override
    public List<Goods> adminFind(StringBuilder where, List<Object> params) {
        List<Goods> goodsList = goodsDao.adminSelect(where,params);
        if (goodsList != null) {
            for (Goods goods : goodsList) {
                Integer typeid = goods.getTypeid();
                GoodsTypeservice goodsTypeservice = new GoodsTypeserviceImpl();
                GoodsType goodsType = goodsTypeservice.findById(typeid);
                goods.setGoodsType(goodsType);
                goodsType.getName();
            }
        }
        return goodsList;
    }

    @Override
    public void adminDeleteGoods(int gid) {
        goodsDao.delete(gid);
    }
}
